﻿/*
 * Copyright (C) 2009-2010 Valerio Fuoglio <valerio.fuoglio@gmail.com>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

using System;
using System.IO;
using System.Threading;
using System.Xml.Linq;

namespace LightScrobbler
{
    internal class Utils
    {
        private static string _apiKey;
        private static string _apiSecret;
        private static string _clientId;

        public static Mutex MutexThreadRequest = new Mutex(false, "LightScrobbler.ThreadRequest");
        public static Semaphore SemaphoreThreadRequest = new Semaphore(5, 5);

        private Utils()
        {
        }

        public static string ApiKey
        {
            get
            {
                if (_apiKey == null) ParseClientConfig();
                return _apiKey;
            }
        }

        public static string ApiSecret
        {
            get
            {
                if (_apiSecret == null) ParseClientConfig();
                return _apiSecret;
            }
        }

        public static string ClientId
        {
            get
            {
                if (_clientId == null) ParseClientConfig();
                return _clientId;
            }
        }


        private static void ParseClientConfig()
        {
            using (
                Stream resourceStream =
                    typeof (Utils).Assembly.GetManifestResourceStream("LightScrobbler.ClientConfig.xml"))
            using (var resourceStreamReader = new StreamReader(resourceStream))
            {
                String xml = resourceStreamReader.ReadToEnd();
                XElement clientConfig = XElement.Parse(xml);

                foreach (XElement x in clientConfig.Elements())
                {
                    switch (x.Name.ToString())
                    {
                        case "ApiKey":
                            _apiKey = x.Value;
                            break;
                        case "ApiSecret":
                            _apiSecret = x.Value;
                            break;
                        case "ClientId":
                            _clientId = x.Value;
                            break;
                    }
                }
            }
        }
    }
}